package panel.downloadZipPanel
{
	import assists.ButtonUtils;
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.caches.SaveZipAsFile;
	import assists.log.Log;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextField;
	
	import main.ClientConstants;
	import main.LocalStoreInfo;
	
	public class DownloadReousrceScenicBox extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var back:Bitmap;
		private var backContainer:Sprite;
		
		private var cityText:TextField;
		
		private var downloadBtn:ButtonUtils;
		
		private var line:ProgressLine;
		
		private var loadZip:SaveZipAsFile;
		
		private var scenic:Object;

		private var store:LocalStoreInfo;
		
		private var cityIsDone:Boolean = false;
		
		public function DownloadReousrceScenicBox(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			store = new LocalStoreInfo;
			
			initThis();
		}
		
		private function initThis():void
		{
			backContainer = new Sprite;
			this.addChild(backContainer);
			back = new _loader.uiManager.componentBack as Bitmap;
			back.width = _stage.stageWidth;
			back.height = _stage.stageHeight * .1;
			backContainer.addChild(back);
			
			cityText = new TextField;
			cityText.mouseEnabled = true;
			cityText.selectable = false;
			cityText.multiline = false;
			cityText.wordWrap = false;
			cityText.x = _stage.stageWidth * .25;
			cityText.y = backContainer.height * .3;
			this.addChild(cityText);
			
			var img:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.downloadIcon,true);
			img.smoothing = true;
			img.height = _stage.stageHeight * .08;
			img.width = _stage.stageHeight * .08;
			downloadBtn = new ButtonUtils(img,downloadThis);
			downloadBtn.x = _stage.stageWidth * .05;
			downloadBtn.y = _stage.stageHeight * .01;
			this.addChild(downloadBtn);
			
			line = new ProgressLine(_stage,_loader,_lang,_uri,_stage.stageWidth,_stage.stageHeight * .01);
			line.x = 0;
			line.y = _stage.stageHeight * .09;
			this.addChild(line);
		}
		
		private function downloadThis():void
		{
			loadZip = new SaveZipAsFile();
			
			var cityName:String = scenic.fileName.split("-")[0] + "-" + scenic.fileName.split("-")[1];
			
			var url:String = _uri.getScenicZip(cityName + "/scenic/" + scenic.fileName);
			loadZip.loadProgress = changeProgressLine;
			loadZip.loadSuccess = loadSuccess;
			loadZip.unzip_init(url,2);
		}
		
		private function changeProgressLine(current:Number,total:Number):void
		{
			line.changeData(current / total);
		}
		
		private function loadSuccess():void
		{
			Log.trace("下载完了,正在解压");
			
			var obj:Object = store.readInfo("downloadInfo");
			if(obj != null){
				obj.downloadList.push(scenic.fileName);
			}else{
				obj = new Object;
				obj = {downloadList:[scenic.fileName]};
				store.storeInfo("downloadInfo",obj);
			}
			
			changeState();
		}
		
		public function changeData(obj:Object,bool:Boolean = false):void
		{
			scenic = obj;
			
			cityIsDone = bool;
			
			TextFieldUtils.setHtmlText(cityText,HtmlText.formatSizeColor("<b>" + scenic.cityName + "</b>",0x000000,18));
			cityText.width = cityText.textWidth + 5;
			
			checkDownload();
		}
		
		private function checkDownload():void
		{
			if(cityIsDone){
				changeState();
			}else{
				var obj:Object = store.readInfo("downloadInfo");
				if(obj){
					for(var i:int = 0;i  < obj.downloadList.length;i++){
						if(obj.downloadList[i] == scenic.fileName){
							changeState();
							return;
						}
					}
				}else{
					return;
				}
			}
		}
		
		private function changeState():void
		{
			downloadBtn.mouseChildren = false;
			downloadBtn.filters = [ClientConstants.GLOBAL_FILTER_GREY];
		
			TextFieldUtils.setHtmlText(cityText,HtmlText.formatSizeColor("<b>" + scenic.cityName + "(资源已下载)" + "</b>",0x000000,18));
			cityText.width = cityText.textWidth + 5;
			
			line.changeData(1);
		}
		
		public override function get height():Number
		{
			return backContainer.height;
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				cityIsDone = false;
				
				downloadBtn.clearThis();
				downloadBtn = null;
				line.clear();
				line = null;
			}
		}
	}
}